#include <cstdio>
#include <algorithm>

using namespace std;

int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};

struct date{
    int year,month,day;
};

struct out
{
    int go, back;
};

bool operator < ( const out &a, const out &b )
{
    return a.go < b.go;
}

inline int leap(int year){
    return (year%4==0&&year%100!=0)||year%400==0;
}

int date2int(date a){
    int ret=a.year*365+(a.year-1)/4-(a.year-1)/100+(a.year-1)/400,i;
    days[1]+=leap(a.year);
    for (i=0;i<a.month-1;ret+=days[i++]);
    days[1]=28;
    return ret+a.day;
}

date int2date(int a){
    date ret;
    ret.year=a/146097*400;
    for (a%=146097;a>=365+leap(ret.year);a-=365+leap(ret.year),ret.year++);
    days[1]+=leap(ret.year);
    for (ret.month=1;a>=days[ret.month-1];a-=days[ret.month-1],ret.month++);
    days[1]=28;
    ret.day=a+1;
    return ret;
}

date res, land;
out o[ 101 ];
int n, rr, ll;

int min( int a, int b ) { return ( a < b )?a :b; }

void init( )
{
    int i;
    date t;
    scanf("%d/%d/%d", &land.month, &land.day, &land.year);
    scanf("%d", &n);
    for ( i = 0; i < n; i++ )
    {
        scanf("%d/%d/%d", &t.month, &t.day, &t.year);
        o[ i ].go = date2int( t );
        scanf("%d/%d/%d", &t.month, &t.day, &t.year);
        o[ i ].back = date2int( t );
    }
    sort( o, o + n );
    rr = date2int( res );
    ll = date2int( land );
    if ( ll - rr > 730 )
        rr = ll - 730;
}

int check( int p )
{
    int left = p + 1094, right = p + 1459, mid, i, c;
//    if ( ( ll - p ) & 1 ) p++;
    while ( left < right )
    {
        mid = ( left + right ) / 2;
        c = ( mid - p + 1 ) * 2;
        if ( p < ll )
            c -= ll - p;
        for ( i = 0; i < n; i++ )
        {
            if ( p > o[ i ].back )
                continue;
            if ( mid >= o[ i ].go )
                if ( mid >= o[ i ].back )
                    if ( o[ i ].go >= ll )
                        c -= 2 * ( o[ i ].back - o[ i ].go + 1 );
                    else
                        c -= o[ i ].back - o[ i ].go + 1;
                else
                    if ( o[ i ].go >= ll )
                        c -= 2 * ( mid - o[ i ].go + 1 );
                    else
                        c -= mid - o[ i ].go + 1;
            else
                break;
        }
        if ( c < 1095 * 2 )
            left = mid + 1;
        else
            right = mid;
    }
    c = 2 * ( left - p + 1 );
    if ( p < ll )
        c -= ll - p;
    for ( i = 0; i < n; i++ )
    {
        if ( p > o[ i ].back )
            continue;
        if ( left >= o[ i ].go )
            if ( left >= o[ i ].back )
                if ( o[ i ].go >= ll )
                    c -= 2 * ( o[ i ].back - o[ i ].go + 1 );
                else
                    c -= o[ i ].back - o[ i ].go + 1;
            else
                if ( o[ i ].go >= ll )
                    c -= 2 * ( left - o[ i ].go + 1 );
                else
                    c -= left - o[ i ].go + 1;
        else
            break;
    }
    if ( c < 1095 * 2 ) return 1000000000;
    return left;
}

void work( )
{
    int i, ans = 1000000000;
    date ret;
    for ( i = rr; i < ll; i++ )
        ans = min( ans, check( i ) );
    ans = min( ans, check( ll ) );
    for ( i = 0; i < n; i++ )
        ans = min( ans, check( o[ i ].back + 1 ) );
    ret = int2date( ans + 1 );
    printf("%d/%d/%d\n", ret.month, ret.day, ret.year);
}

int main( )
{
    while ( scanf("%d/%d/%d", &res.month, &res.day, &res.year) != EOF )
    {
        init( );
        work( );
    }
    return 0;
}
